package com.quectel.taskserver.job.partybuilding;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.partybuilding.PartyPersonMsgBusiness;
import com.quectel.constant.core.partybuilding.PartyBuildingPlanConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.core.module.partybuildingtask.dto.PartyBuildingPlanDto;
import com.quectel.core.module.partybuildingtask.service.PartyBuildingPlanService;
import com.quectel.core.module.partybuildingtask.service.PartyBuildingPlanTaskService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.PageFetchKit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 党建任务生成
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-12 8:59:00
 */
@Component
public class PartyBuildingPlanTaskGenerateTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingPlanTaskGenerateTask.class);

    public static final String GEN_TASK_TIME = "genTaskTime";

    @DubboReference
    private PartyBuildingPlanService partyBuildingPlanService;
    @DubboReference
    private PartyBuildingPlanTaskService partyBuildingPlanTaskService;
    @DubboReference
    private PartyPersonService partyPersonService;
    @DubboReference
    private MobileUserService mobileUserService;
    @Autowired
    private PartyPersonMsgBusiness partyPersonMsgBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        generateWeekTask();
        generateMonthTask();
        generateMonthEndTask();
    }

    /**
     * 最大提醒时间是24小时 提前26小时生成任务
     */
    private void generateWeekTask() {
        LOGGER.info("党建按周任务，开始执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

        Map<String,Object> params = new HashMap<>(4);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriodType),PartyBuildingPlanConstants.PeriodType.WEEK.getType());
        //提前26小时生成
        DateTime offset = DateUtil.offsetHour(new Date(), 26);
        //当天是周几
        int i = offset.dayOfWeek();
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriod),String.valueOf(i));
        //未取消
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getCancel),SystemConstants.NO);
        params.put(GEN_TASK_TIME,DateUtils.format(offset, DateUtils.FormatType.COMMON));
        //查询符合条件的
        List<PartyBuildingPlanDto> partyBuildingPlanDtos = PageFetchKit.fetchAll(r -> partyBuildingPlanService.queryList(r), params);

        if(CollectionUtils.isEmpty(partyBuildingPlanDtos)){
            return;
        }
        for (PartyBuildingPlanDto partyBuildingPlanDto : partyBuildingPlanDtos){
            //生成task
            partyBuildingPlanTaskService.genTask(partyBuildingPlanDto, offset.toJdkDate());
        }
        LOGGER.info("党建按周任务，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

    }
    private void generateMonthTask() {
        LOGGER.info("党建按月任务，开始执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

        Map<String,Object> params = new HashMap<>(4);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriodType),PartyBuildingPlanConstants.PeriodType.MONTH.getType());
        //提前26小时生成
        DateTime offset = DateUtil.offsetHour(new Date(), 26);
        //当天是几号
        int i = offset.dayOfMonth();
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriod),String.valueOf(i));
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getCancel),SystemConstants.NO);
        params.put(GEN_TASK_TIME,DateUtils.format(offset, DateUtils.FormatType.COMMON));
        //查询符合条件的
        List<PartyBuildingPlanDto> partyBuildingPlanDtos = PageFetchKit.fetchAll(r -> partyBuildingPlanService.queryList(r), params);
        for (PartyBuildingPlanDto partyBuildingPlanDto : partyBuildingPlanDtos){
            //生成task
            partyBuildingPlanTaskService.genTask(partyBuildingPlanDto, offset.toJdkDate());
        }
        LOGGER.info("党建按月任务，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

    }

    private void generateMonthEndTask() {
        LOGGER.info("党建每月最后一天任务，开始执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

        Map<String,Object> params = new HashMap<>(4);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriodType),PartyBuildingPlanConstants.PeriodType.MONTH.getType());
        //提前26小时生成
        DateTime offset = DateUtil.offsetHour(new Date(), 26);
        //判断是否是月末
        boolean lastDayOfMonth = DateUtil.isLastDayOfMonth(offset);
        if(!lastDayOfMonth){
            LOGGER.info("党建每月最后一天任务，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));
            return;
        }
        //查询每月末执行的任务
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getPeriod),PartyBuildingPlanConstants.END_OF_MONTH_VALUE);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanDto::getCancel),SystemConstants.NO);
        params.put(GEN_TASK_TIME,DateUtils.format(offset, DateUtils.FormatType.COMMON));

        //查询符合条件的
        List<PartyBuildingPlanDto> partyBuildingPlanDtos = PageFetchKit.fetchAll(r -> partyBuildingPlanService.queryList(r), params);
        for (PartyBuildingPlanDto partyBuildingPlanDto : partyBuildingPlanDtos){
            //生成task
            partyBuildingPlanTaskService.genTask(partyBuildingPlanDto, offset.toJdkDate());
        }
        LOGGER.info("党建每月最后一天任务，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));



    }

}
